package LevelEditor;

import Graphics.XmlFileFilter;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *  Panel that represents the toolbar containing New, Open, and Save buttons
 * @author jh20
 */
public class FunctionPanel extends JPanel {

    private MainPanel mainPanel;

    /**
     * A constructor
     * @param mp
     */

    public FunctionPanel(MainPanel mp) {
        mainPanel = mp;

        this.add(getButtonPanel());
    }

    /**
     * Factory method that returns functional New, Open, and Save buttons
     * @return
     */

    public JPanel getButtonPanel() {
        JPanel miniP = new JPanel();
        miniP.setLayout(new GridLayout(1,3));

        JButton newButton = new JButton("New");
        newButton.addActionListener(new NewListener());
        miniP.add(newButton);

        JButton openButton = new JButton("Open");
        openButton.addActionListener(new OpenListener());
        miniP.add(openButton);

        JButton saveButton = new JButton("Save");
        saveButton.addActionListener(new SaveListener());
        miniP.add(saveButton);

        JPanel buttonP = EditorFunctions.getSpace(miniP);
        return buttonP;
    }

    /**
     * ActionListener for the New button
     */

    private class NewListener implements ActionListener {

        // when clicked, the button brings up a dialog asking the user to confirm

        @Override
        public void actionPerformed(ActionEvent e) {
            if (JOptionPane.showConfirmDialog(new JFrame(), "Are you sure you want to erase your current data and create a new level?", "Confirm Data Reset", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                mainPanel.resetPreview();
                mainPanel.resetLevel();
            }
             
        }
        
    }

    /**
     * ActionListener for the Open button
     */

    private class OpenListener implements ActionListener {

        // when clicked, brings up a file chooser dialog choosing where to open

        @Override
        public void actionPerformed(ActionEvent e) {
            File openedFile = null;
            JFileChooser fileC = new JFileChooser(Graphics.MainGui.levelpath);

            int returnVal = fileC.showOpenDialog(new JFrame());
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                openedFile = fileC.getSelectedFile();
            }
            if (openedFile != null) {
                System.out.println("Opening " + openedFile.getName() + "...");
                mainPanel.read(openedFile.getAbsolutePath());
            }
        }
        
    }

    /**
     * ActionListener for the Save button
     */

    private class SaveListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            File saveToFile = null;

            JFileChooser fileC = new JFileChooser(Graphics.MainGui.levelpath);
            int returnVal = fileC.showSaveDialog(new JFrame());
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                saveToFile = fileC.getSelectedFile();
            }
            if (saveToFile != null) {
                System.out.println("Saving to " + saveToFile.getName() + "...");
                mainPanel.write(saveToFile.getAbsolutePath());
            }
        }
        
    }

}
